//
//  MDetailSettingViewController.m
//  OneCreditTreasure
//
//  Created by iOS Tedu on 16/8/25.
//
//

#import "MDetailSettingViewController.h"
//实名认证
#import "RealNameViewController.h"
//更换手机号
#import "ChangePhoneNumberViewController.h"

@interface MDetailSettingViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *myTableView;
@property (nonatomic, strong) UILabel *accessoryLB;
@property(nonatomic,strong) NSArray<NSArray *> *titleArray;

@property (nonatomic, strong) UILabel *certificationLB;
@property (nonatomic, strong) UILabel *trachSizeLB;
@end

@implementation MDetailSettingViewController
#pragma mark - 生命周期 life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self myTableView];
}

- (instancetype)init {
    if (self = [super init]) {
        self.title = @"设置";
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (_certificationLB) {
        self.certificationLB.text = [[NSUserDefaults standardUserDefaults] boolForKey:@"认证信息"] == YES ? @"已认证" : @"未认证";
    }
}

#pragma mark - UITableView dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.titleArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.titleArray[section].count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"setCell"];
    cell.textLabel.text = self.titleArray[indexPath.section][indexPath.row];
    cell.textLabel.font = defaultFont;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    if ((indexPath.section == 0 && indexPath.row == 0) || indexPath.section == 2) {
        UILabel *label = [UILabel new];
        label.font = defaultFont;
        label.text = (indexPath.section == 0 ? @"未认证" : @"200M");
        label.textColor = [UIColor lightGrayColor];
        [cell.contentView addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(-12);
            make.centerY.equalTo(0);
        }];
        
        if (indexPath.section == 0 && indexPath.row == 0) {
            self.certificationLB = label;
        }
        if (indexPath.section == 2) {
            self.trachSizeLB = label;
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
        
    } else if (indexPath.section == 1 && indexPath.row > 1) {
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        UISwitch *mySW = [[UISwitch alloc] init];
        [cell.contentView addSubview:mySW];
        [mySW mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(-20);
            make.centerY.equalTo(0);
        }];
        mySW.tag = (indexPath.row == 2 ? 1000 : 1001);
        [mySW addTarget:self action:@selector(changeLockStyle:) forControlEvents:UIControlEventValueChanged];
        
        mySW.on = [[NSUserDefaults standardUserDefaults] boolForKey:(indexPath.row == 2 ? @"指纹支付" : @"指纹解锁")];
    }
    
    return cell;
}

#pragma mark - UITableView delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            //  实名认证
            RealNameViewController *realVC = [RealNameViewController new];
            [self.navigationController pushViewController:realVC animated:YES];
        } else {
            //更换手机号
            ChangePhoneNumberViewController *vc = [ChangePhoneNumberViewController new];
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10 * AUTO_WIDTH;
}

#pragma mark - 方法 methods
- (void)changeLockStyle:(UISwitch *)sender {
    [[NSUserDefaults standardUserDefaults] setBool:sender.isOn forKey:(sender.tag == 1000 ? @"指纹支付" : @"指纹解锁")];
    if (sender.isOn) {
        [self.view showWarning:(sender.tag == 1000 ? @"指纹支付开启成功" : @"指纹解锁开启成功")];
    } else {
        [self.view showWarning:(sender.tag == 1000 ? @"指纹支付关闭成功" : @"指纹解锁关闭成功")];
    }
}

#pragma mark - 懒加载 lazy loading
- (UITableView *)myTableView {
    if(_myTableView == nil) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        [self.view addSubview:_myTableView];
        [_myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(0);
        }];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        
        [_myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"setCell"];
    }
    return _myTableView;
}

- (NSArray<NSArray *> *)titleArray {
    if(_titleArray == nil) {
        _titleArray = @[@[@"实名认证",@"更换手机号"],@[@"更改登陆密码",@"更改支付密码",@"使用Touch ID支付",@"使用Touch ID解锁"],@[@"清除缓存"]];
    }
    return _titleArray;
}
@end
